package primetest.impl.controler;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.security.RolesAllowed;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import primetest.dto.DepartmentDto;
import primetest.dto.DoctorDto;
import primetest.dto.PatientDto;
import primetest.dto.TreatmentRecordDto;
import primetest.faces.TreatmentRecordListModel;
import primetest.impl.manager.SessionManager;
import primetest.impl.service.DoctorService;
import primetest.impl.service.PatientService;
import primetest.impl.service.TreatmentRecordService;

@Named("treatmentRecordListControler")
@SessionScoped
@RolesAllowed({"treatment_record", "admin"})
public class TreatmentRecordListControler implements Serializable {

	/**
	 *
	 */
	private static final long serialVersionUID = -118678570623180842L;

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	/*
	 * 'Conversation' object required in order to start a conversation scope See
	 * boolList.xhtml
	 */
	@Inject
	private SessionManager sessionManager;


	private TreatmentRecordListModel treatmentRecordList = null;
	private List<TreatmentRecordDto> baseTreatmentRecordList;
	private TreatmentRecordDto selectedTreatmentRecord = new TreatmentRecordDto();
	private TreatmentRecordDto backTreatmentRecord = new TreatmentRecordDto();
	private TreatmentRecordDto newTreatmentRecord = new TreatmentRecordDto();
	private int first;
	private boolean editMode = false;
	private boolean addMode = false;
	private List<TreatmentRecordDto> filteredTerms;

	public boolean getEditMode() {
		return editMode;
	}

	public boolean getAddMode() {
		return addMode;
	}

	@Inject
	private TreatmentRecordService treatmentRecordService;
	@Inject
	private PatientService patientService;
	@Inject
	private DoctorService doctorService;


	private List<DepartmentDto> departmentList = null;
	private List<DoctorDto> doctorList = null;
	private List<PatientDto> patientList = null;

	@RolesAllowed({"treatment_record", "admin"})
	public TreatmentRecordListModel getTreatmentRecordList() {
		if (baseTreatmentRecordList == null) {
			try {
				baseTreatmentRecordList = treatmentRecordService
						.getTreatmentRecordList();
				treatmentRecordList = new TreatmentRecordListModel(
						baseTreatmentRecordList);
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}

		return baseTreatmentRecordList == null ? new TreatmentRecordListModel(
				new ArrayList<TreatmentRecordDto>()) : treatmentRecordList;
	}

	public void deleteTreatmentRecord() throws IOException {
		treatmentRecordService.deleteTreatmentRecord(selectedTreatmentRecord
				.getSeq());
	}


	public TreatmentRecordDto getSelectedTreatmentRecord() {
		return selectedTreatmentRecord;
	}

	public void setSelectedTreatmentRecord(
			TreatmentRecordDto selectedTreatmentRecord) {
		this.selectedTreatmentRecord = selectedTreatmentRecord;
	}

	public int getFirst() {
		return first;
	}

	public void setFirst(int first) {
		this.first = first;
	}



	public TreatmentRecordDto getNewTreatmentRecord() {
		return newTreatmentRecord;
	}

	public void setNewTreatmentRecord(TreatmentRecordDto newTreatmentRecord) {
		this.newTreatmentRecord = newTreatmentRecord;
	}

	/**
	 *
	 */
	public void saveEditTreatmentRecord() {
		try {
			if (selectedTreatmentRecord.isValid()) {
				selectedTreatmentRecord.save();
				selectedTreatmentRecord.setDoctorDto(doctorService.findDoctorBySeq(selectedTreatmentRecord.getDoctorSeq()));
				selectedTreatmentRecord.setPatientDto(patientService.getPatient(selectedTreatmentRecord.getPatientSeq()));
				treatmentRecordService
						.updateTreatmentRecord(selectedTreatmentRecord);
				logger.debug("save the selectedTreatmentRecord" + " sympton: "
						+ selectedTreatmentRecord.getSymptom() + " seq: "
						+ selectedTreatmentRecord.getSeq());
				editMode = false;
			} else {
				sessionManager.addGlobalMessageFatal("TreatDate invalid", null);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void startEditTreatmentRecord() {
		editMode = true;
		backTreatmentRecord.setAll(selectedTreatmentRecord);
	}

	public void cancelEditTreatmentRecord() {
		editMode = false;
		selectedTreatmentRecord.setAll(backTreatmentRecord);
	}

	public void startAddTreatmentRecord() {
		addMode = true;
		newTreatmentRecord = new TreatmentRecordDto();
	}

	public void addNewTreatmentRecord() throws IOException {
		if (newTreatmentRecord.isValid()) {
			newTreatmentRecord.save();
			newTreatmentRecord.setDoctorDto(doctorService.findDoctorBySeq(newTreatmentRecord.getDoctorSeq()));
			newTreatmentRecord.setPatientDto(patientService.getPatient(newTreatmentRecord.getPatientSeq()));
			treatmentRecordService.addTreatmentRecord(newTreatmentRecord);
			addMode = false;
		} else {
			sessionManager.addGlobalMessageFatal("Build Date invalid", null);
		}
	}

	public void cancelAddTreatmentRecord() {
		addMode = false;
	}

	public List<DepartmentDto> getDepartmentList() {
		return departmentList;
	}

	public void setDepartmentList(List<DepartmentDto> departmentList) {
		this.departmentList = departmentList;
	}

	public List<DoctorDto> getDoctorList() throws IOException {
		doctorList = doctorService.getDoctorList();
		return doctorList;
	}

	public List<PatientDto> getPatientList() throws IOException {
		patientList = patientService.getPatientList();
		return patientList;
	}

	public List<TreatmentRecordDto> getFilteredTerms() {
		return filteredTerms;
	}

	public void setFilteredTerms(List<TreatmentRecordDto> filteredTerms) {
		this.filteredTerms = filteredTerms;
	}
}
